/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekdb;

/**
 *
 * @author localhost
 */
import java.sql.*;
public class koneksi {
   private String driverName="com.mysql.jdbc.Driver";
   private String jdbcType="jdbc:mysql";

   private static String host="localhost";
   private static String dataBaseName="sitisunat";
//   private static String tableName="mahasiswa";

   private static String password="";
   private static String userId="root";

   private Connection conn;
   private boolean isConnect=false;
    

   //Constructor
   public koneksi(){

   }

   public void setDataBaseName(String value){
      dataBaseName=value;
   }
   
//   public void setTableName(String value){
//      tableName=value;
//   }

   public void setPassword(String value){
      password=value;
   }

   public void setUserId(String value){
      userId=value;
   }

   public void setHostName(String value){
      host=value;
   }

   
   
    public String getDataBaseName() {
        return dataBaseName;
    }
    
//    public String getTableName() {
//        return tableName;
//    }

    public String getDriverName() {
        return driverName;
    }

    public String getHost() {
        return host;
    }

    public String getPassword() {
        if(password==null)
    password.equals("");
        return password;
    }

    public String getUserId() {
        return userId;
    }
   
   

   public void setkoneksi(){

      String currentUrl=jdbcType+"://"+this.getHost()+"/"+this.getDataBaseName()+"?user="+
         this.getUserId()+"&password="+this.getPassword();

     	try {
	    Class.forName(driverName);
       conn = DriverManager.getConnection(currentUrl, this.getUserId(), this.getPassword());
       isConnect=true;
		}

      catch(ClassNotFoundException cnfe)
	   {
        System.err.println(cnfe);
      }

      catch(SQLException sqle)
      {
        conn=null;
        isConnect=false;
        System.err.println(sqle);
      }
   }
   

   public boolean isConnected(){
      return isConnect;
   }

   public Connection getConnection(){

      if (conn !=null)
         return conn;
      else //Try again
      {
         setkoneksi();
         return conn;
      }
   }

   public void close()throws SQLException {

      conn.close();
   }

   
}
